import {
  _decorator,
  Component,
  Node,
  UITransform,
  Sprite,
  Animation,
  SpriteFrame,
  AnimationClip,
  animation,
  utils,
} from 'cc'
const { ccclass, property } = _decorator

// 引入事件中心
import EventManager from '../../Runtime/EventManager'
import DataManager from '../../Runtime/DataManager'

// 实体类型
import { EntityManager } from '../../Base/EntityManager'
import { IEntity } from '../../Levels'
// 状态机
import { DoorStateMchine } from './DoorStateMchine'

// 状态机类型
import { PLAYER_STATE_TYPE_ENUM, ENTITY_TYPE_ENUM } from '../../Enums'


import { DIRECTION } from '../../Enums'

@ccclass('DoorManager')
export class DoorManager extends EntityManager {
  id: string
  async init(params: IEntity) {
    // 初始化状态机
    this.fsm = this.addComponent(DoorStateMchine)
    await this.fsm.init()

    // 传入默认值
    super.init(params)

    // 注册事件
    EventManager.Instance.on('DOOR_OPEN', this.onDeath, this)
  }

  onDestroy(): void {
    super.onDestroy()

    // 移除
    EventManager.Instance.off('DOOR_OPEN', this.onDeath)
  }

  // 门打开
  onDeath() {
    if (this.state === PLAYER_STATE_TYPE_ENUM.DEATH) {
      return
    }

    let woodenskeletons = DataManager.Instance.woodenskeletons
    if (woodenskeletons.every((item) => item.state === PLAYER_STATE_TYPE_ENUM.DEATH)) {
      this.state = PLAYER_STATE_TYPE_ENUM.DEATH
    }
  }
}
